package cn.yyh.org.service.impl;

import cn.yyh.basic.service.impl.BaseServiceImpl;
import cn.yyh.org.domain.Department;
import cn.yyh.org.mapper.DepartmentMapper;
import cn.yyh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: DepartmentServiceImpl
 * @Package: cn.yyh.org.service.impl
 * @date 2021/6/18
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<DepartmentMapper,Department> implements IDepartmentService {


    @Override
    public void save(Department department) {
        baseMapper.save(department);
        // 因为添加和修改时都要给部门动态生成dirPath路径,所以调用修改方法
        this.update(department);
    }

    @Override
    public void update(Department department) {
        // 当上级部门的id为空,则表示现在的对象是顶级部门,直接拼接自己部门的id
        if (department.getParent() == null || department.getParent().getId() == null) {

            department.setDirPath("/" + department.getId());
        }else {
            // 不为空则表示有上级部门,获取到上级部门的dirPath,和现在部门的id拼接
            Department byId = baseMapper.getById(department.getParent().getId());
            department.setDirPath(byId.getDirPath() + "/" + department.getId());
        }

        baseMapper.update(department);
    }

    @Override
    public List<Department> listDeptTree(Long id) {
        return baseMapper.listDeptTree(id);
    }

}
